/**
 * 
 */
package server;

import java.sql.DriverManager;

/**
 * @author Ingo Philipp
 *
 * @version $Revision: 1.0 $
 */
public class JdbcMySQLConnector implements IDBConnector {

    private static String URL_PREFIX = "jdbc:mysql://";

    private final DBProperties mySqlDbProperties = null;
    private java.sql.Connection dbConnection = null;
    private static String MYSQL_DRIVER = "com.mysql.jdbc.Driver";
    private static JdbcMySQLConnector instance = null;

    public static JdbcMySQLConnector getInstance() {
	if (instance == null) {
		    instance = new JdbcMySQLConnector();
	}
	return instance;
    }

    @Override
    public void connect(DBProperties dbProps) throws DBConException {
	try {
	    Class.forName(MYSQL_DRIVER);
	    this.dbConnection = DriverManager.getConnection(URL_PREFIX
		    + dbProps.getHost() + ":" + dbProps.getPort() + "/"
		    + dbProps.getDbName(), dbProps.getUserName(),
		    dbProps.getPassword());
	} catch (Exception e) {
	    throw new DBConException(e.getMessage());
	}
    }

    @Override
    public void disconnect() {
	this.dbConnection = null;
    }

    @Override
    public Boolean isConnected() {
	return (this.dbConnection != null);
    }

    @Override
    public DBProperties getCurrentDBProperties() throws DBConException {
	return this.dbProperties;
    }

    @Override
    public <ConType> ConType getCurConnection() {
	return (ConType) this.dbConnection;
    }


}
